import numpy as np


with open("data.csv", 'r') as f:
    data = np.loadtxt(f, delimiter=",")  # 读取CVS数据
    x = data[:, 0:3]  # 前三列为x
    y = np.array(data[:, 3])  # 最后一列为y
    x = np.c_[np.ones(len(x)), data[:, 0:3]]  # 添加一列 1
    theta = np.linalg.inv(x.transpose().dot(x)).dot(x.transpose()).dot(y)  # 正规方程法
    # theta = np.linalg.lstsq(x, y, rcond=None)[0]  # 最小二乘法
    a, b, c, d = theta

    print(a, b, c, d)
